package com.learn;


import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

class Solution {
    public boolean isValid(String s) {
        // 边界条件
        int sLen = s.length();
        if (sLen == 1) {
            return false;
        }

        //   不同括号的对应
        Map<Character, Character> store = new HashMap<>();

        store.put(')', '(');
        store.put('}', '{');
        store.put(']', '[');
        //   使用栈的思想解决
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < sLen; i++) {
            //     如果头顶又对应，那么直接去掉顶部，接着走，如果没有则加入栈
            char now = s.charAt(i);
            if (!stack.isEmpty()) {
                if (store.get(now) == stack.peek()) {
                    stack.pop();
                } else {
                    return false;
                }
            } else {
                stack.push(now);
            }
        }
        return stack.isEmpty();
    }
}